@extends('admin.layouts.app')

@section('title', '仪表盘 - 挪车小助手后台管理')

@section('content')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">仪表盘</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="window.print()">
                <i class="bi bi-printer"></i> 打印
            </button>
        </div>
    </div>
</div>

<div class="row row-cols-1 row-cols-md-4 g-4 mb-4">
    <div class="col">
        <div class="card text-white bg-primary">
            <div class="card-body">
                <h5 class="card-title">总用户数</h5>
                <p class="card-text fs-1">{{ $stats['users_count'] }}</p>
            </div>
        </div>
    </div>
    <div class="col">
        <div class="card text-white bg-success">
            <div class="card-body">
                <h5 class="card-title">已注册车主</h5>
                <p class="card-text fs-1">{{ $stats['car_owners_count'] }}</p>
            </div>
        </div>
    </div>
    <div class="col">
        <div class="card text-white bg-info">
            <div class="card-body">
                <h5 class="card-title">扫码总次数</h5>
                <p class="card-text fs-1">{{ $stats['scans_count'] }}</p>
            </div>
        </div>
    </div>
    <div class="col">
        <div class="card text-white bg-warning">
            <div class="card-body">
                <h5 class="card-title">电话拨打次数</h5>
                <p class="card-text fs-1">{{ $stats['calls_count'] }}</p>
            </div>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                最近30天扫码趋势
            </div>
            <div class="card-body">
                <canvas id="scanChart" height="100"></canvas>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span>最近扫码记录</span>
                <a href="{{ route('admin.scan-logs.index') }}" class="btn btn-sm btn-outline-primary">查看全部</a>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-sm">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>扫码用户</th>
                                <th>车主</th>
                                <th>扫码时间</th>
                                <th>是否拨打</th>
                                <th>IP地址</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($recentScans as $scan)
                                <tr>
                                    <td>{{ $scan->id }}</td>
                                    <td>{{ $scan->scanner ? $scan->scanner->nickname : '未知用户' }}</td>
                                    <td>{{ $scan->carOwner ? ($scan->carOwner->user->nickname ?? '未知') : '未知车主' }}</td>
                                    <td>{{ $scan->scan_time }}</td>
                                    <td>{!! $scan->call_made ? '<span class="badge bg-success">是</span>' : '<span class="badge bg-secondary">否</span>' !!}</td>
                                    <td>{{ $scan->ip_address }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="6" class="text-center">暂无扫码记录</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const scanData = {!! json_encode($scansByDate) !!};
        const labels = scanData.map(item => item.date);
        const data = scanData.map(item => item.count);
        
        const ctx = document.getElementById('scanChart').getContext('2d');
        const myChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: labels,
                datasets: [{
                    label: '扫码次数',
                    data: data,
                    backgroundColor: 'rgba(13, 110, 253, 0.2)',
                    borderColor: 'rgba(13, 110, 253, 1)',
                    borderWidth: 1,
                    tension: 0.1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            precision: 0
                        }
                    }
                },
                plugins: {
                    legend: {
                        position: 'top',
                    },
                    title: {
                        display: true,
                        text: '每日扫码统计'
                    }
                }
            }
        });
    });
</script>
@endsection 